package com.bang.study.query;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @ClassName: DismaxSearch
 * @author: BANG
 * @Date: 2020/01/15 09:54
 * @Version: 1.0
 * @Modified By:
 * @Desc: TODO
 */
public class MulitCrossFieldsSearch {

    public static void main(String[] args) throws UnknownHostException {
        Settings settings = Settings.builder().build();

        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)
        );


        SearchResponse searchResponse = client.prepareSearch("forum").setTypes("article")
                .setQuery(QueryBuilders.multiMatchQuery("Peter Smith","author_first_name", "author_last_name" )
                                .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).operator(Operator.AND)
                        )
                .get();

        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            System.err.println(searchHit.getSourceAsString());
        }
    }
}
